import { Component, Input } from '@angular/core';
import { CascaderItem } from 'ng-devui';

@Component({
  selector: 'app-supplier-class-edit',
  templateUrl: './supplier-class-edit.component.html',
  styleUrls: ['./supplier-class-edit.component.scss']
})
export class SupplierClassEditComponent {
  @Input() data: any
  form: any = {
    supplyClassId:null,
    supplyClassName: "",
    note: "",
    parentSupplyClassId: null,
    parentSupplyClassName: "",
    subSupplyClasses:[]
}
  options = []
  pathPids: any = []
  constructor() { }
  ngOnInit(): void {
    this.form = JSON.parse(JSON.stringify(this.data.option));
    this.options = this.convertToCascaderItem(this.data.options)
    this.pathPids = this.data.pathPids
  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.supplyClassName,
        value: item.supplyClassId,
        disabled: item.supplyClassId == this.form.supplyClassId
      };
      if (item.subSupplyClasses) {
        cascaderItem.children = this.convertToCascaderItem(item.subSupplyClasses);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  ngModelChange() {
    this.form.parentSupplyClassId = this.pathPids[this.pathPids.length - 1]
  }
}
